/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.edu.ufcg.boombox.controller;

import br.edu.ufcg.boombox.view.LoginView;
import br.edu.ufcg.boombox.view.PaginaInicialBoomBoxView;
import br.edu.ufcg.boombox.view.CadastroUsuarioView;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import br.edu.ufcg.boombox.model.LoginModel;
import br.edu.ufcg.boombox.model.RepositoryManager;
import br.edu.ufcg.boombox.model.Usuario;

/**
 *
 * @author Targaryen
 */
public class LoginController implements ActionListener, Inicializavel {
    
    
    // Model
    
    
    // View
    private LoginView loginView;
    
    //Contexto

    public LoginController(LoginView loginView) {
        this.loginView = loginView;
        this.loginView.configuraOuvinte(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        
        if (e.getActionCommand().equals("logar")) {
            
            LoginModel modelo = this.loginView.lerModelo();
            String idSessao = null;
            if(modelo != null) {
                try {
                    System.out.println("contexto:"+ContextoDaAplicacao.getInstance());
                    System.out.println(modelo == null);
                    
                    idSessao = ContextoDaAplicacao.getInstance().logar(modelo.getEmail(), modelo.getSenha());
                
                    if(modelo == null) throw new FalhaDeAutenticacaoException();
                    this.loginView.clear();
                    this.loginView.dispose();
                    ContextoDaAplicacao.getInstance().getControlador("paginaInicialBoomBoxController").inicializar();
                    System.out.println("entrou na pagina inicial");
                
                } catch (FalhaDeAutenticacaoException ex) {
                    JOptionPane.showMessageDialog(null, "Login ou senha inválidos!");
                }
            }
        } else if( e.getActionCommand().equals("cadastrar")) {
            
            ContextoDaAplicacao.getInstance().getControlador("cadastroUsuarioController").inicializar();
            this.loginView.dispose();
            this.loginView.clear();
            
        }
        
        
    }

    @Override
    public void inicializar() {
        this.loginView.setVisible(true);
    }
    
}
